//package com.crm.framework.interceptor.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.crm.common.utils.StringUtils;
//import com.crm.common.utils.sign.SignUtil;
//
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import java.time.Instant;
//import java.util.Enumeration;
//import java.util.HashMap;
//import java.util.Map;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import lombok.extern.slf4j.Slf4j;
//
///**
// * 账单平台拦截器
// */
//@Slf4j
//@Component
//public class BillPlatformSignatureInterceptor implements HandlerInterceptor {
//    @Value("${crmAdminOpen.api.secret}")
//    private String secret;
//
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//        String sign = request.getParameter("sign");
//        if (StringUtils.isEmpty(sign)) {
//            throw new RuntimeException("签名错误");
//        }
//        log.info("SignatureInterceptor.sign:{}",sign);
//        String timestamp = request.getParameter("timestamp");
//        // 时间戳要为10位的秒级时间戳
//        if (timestamp.length() != 10) {
//            throw new RuntimeException("timestamp时间戳格式错误");
//        }
//        log.info("SignatureInterceptor.timestamp:{}",timestamp);
//        //check时间戳的值是否在当前时间戳前后一30秒以内
//        String currTimestamp = String.valueOf(Instant.now().getEpochSecond());
//        int currTimestampNum = Integer.parseInt(currTimestamp);
//        int verifyTimestampNum = 0;
//        try {
//            verifyTimestampNum =  Integer.parseInt(timestamp);
//        } catch (NumberFormatException e) {
//            throw new RuntimeException("timestamp时间戳格式错误");
//        }
//        log.info("verifyTimestampNum:" + verifyTimestampNum + "-currTimestampNum:" + currTimestampNum + "result:" + Math.abs(verifyTimestampNum - currTimestampNum));
//        // 在30秒范围之外，访问已过期
//        if (Math.abs(verifyTimestampNum - currTimestampNum) > 300) {
//            throw new RuntimeException("签名已经过期");
//        }
//
//        Enumeration<?> pNames = request.getParameterNames();
//        Map<String, String> params = new HashMap<>();
//        while (pNames.hasMoreElements()) {
//            String pName = (String) pNames.nextElement();
//            if ("sign".equals(pName)) {
//                continue;
//            }
//            if ("null".equals(pName)) {
//                continue;
//            }
//            if("menus".equals(pName)){
//                continue;
//            }
//            String pValue = request.getParameter(pName);
//            params.put(pName, pValue);
//        }
//
//        log.info("params.orgId：{}",params.get("orgId"));
//        log.info("params.timestamp：{}",params.get("timestamp"));
//        log.info("params：{}", JSONObject.toJSONString(params));
//        String newSign = SignUtil.getSign(params, secret);
//        log.info("newSign:{}",newSign);
//
//        if (sign.equals(SignUtil.getSign(params, secret))) {
//            return true;
//        } else {
//            throw new RuntimeException("签名错误");
//        }
//    }
//
//}
